﻿
#region Using Directives.

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Globalization;
using System.Linq;
using System.Windows.Forms;
using BrightIdeasSoftware;
using GDFlacTool.Common;
using GDFlacTool.Properties;

#endregion

namespace GDFlacTool.Forms
{
    public partial class FormPreviewTagFromFile : Form
    {
        #region Fields.

        private readonly TypedObjectListView<TagFileObject> _trackList;
        private readonly IList<Track> _tracks;
        private readonly MaskHelperCollection _masks;
        private readonly bool _replaceUnderscores;
        private readonly bool _useCase;
        private readonly CaseType _caseType;
        private readonly string _currentPath;
        private readonly TagFromFileHelper _helper;
        private readonly string _currentExtension = string.Empty;

        #endregion

        #region Constructors.

        public FormPreviewTagFromFile(IList<Track> tracks, MaskHelperCollection masks, bool replaceUnderscores, bool useCase, CaseType caseType, string currentPath, string extension)
        {
            InitializeComponent();

            Width = PreviewTagFromFileSettings.Default.FormWidth;
            Height = PreviewTagFromFileSettings.Default.FormHeight;

            _tracks = tracks;
            _masks = masks;
            _replaceUnderscores = replaceUnderscores;
            _useCase = useCase;
            _caseType = caseType;
            _currentPath = currentPath;
            _currentExtension = extension;

            _trackList = new TypedObjectListView<TagFileObject>(objectListView);

            _helper = new TagFromFileHelper(_masks.Mask, _replaceUnderscores, _useCase, _caseType);

            LoadMaskCombo();

            int index = cboFileNameMask.Items.IndexOf(_masks.Mask);

            if (index >= 0)
            {
                cboFileNameMask.SelectedIndex = index;
            }
            else
            {
                cboFileNameMask.Text = _masks.Mask;
            }
        }

        #endregion

        #region Properties.

        public string GetCurrentMask
        {
            get { return cboFileNameMask.Text; }
        }

        public List<TagHelper> GetTagHelpers
        {
            get { return _helper.GetTagHelper; }
        }

        public int CountTagChanges
        {
            get { return _helper.GetTagHelper.Count; }
        }

        public IList<TagFileObject> GetTagFileObjects
        {
            get { return _trackList.Objects; }
        }

        #endregion

        #region Form Events.

        private void FormPreviewFileName_Load(object sender, EventArgs e)
        {
            RefreshMask();
        }

        private void FormPreviewFileName_FormClosing(object sender, FormClosingEventArgs e)
        {
            PreviewTagFromFileSettings.Default.FormHeight = Height;
            PreviewTagFromFileSettings.Default.FormWidth = Width;

            PreviewTagFromFileSettings.Default.Save();
        }

        #endregion

        #region Public Methods.

        #endregion

        #region Private Methods

        private void CreateColumns()
        {
            objectListView.AllColumns.RemoveRange(1, objectListView.AllColumns.Count - 1);

            foreach (TagHelper tagHelper in _helper.GetTagHelper)
            {
                if(tagHelper.IsTag && tagHelper.TagType != TagType.Remover)
                {
                    var col = new OLVColumn(tagHelper.Name, "");

                    col.Width = tagHelper.ColumnWidth;

                    col.AspectName = tagHelper.AspectName;

                    objectListView.AllColumns.Add(col);

                    objectListView.RebuildColumns();
                }
            }
        }

        private void RefreshMask()
        {
            if (_helper != null)
            {
                _helper.Refresh(cboFileNameMask.Text);

                CreateColumns();

                LoadObjectListView();
            }

            txtResultPreview.Text = string.Format("{0}{1}", ShowPreView(_masks.Mask), _currentExtension);
        }

        private void LoadObjectListView()
        {
            List<TagFileObject> objects = _helper.GetTagFromFilename(_tracks, _helper.GetTagHelper);

            objectListView.SetObjects(objects);
        }

        private void btnMaskEditor_Click(object sender, EventArgs e)
        {
            var frm = new FormMaskEditor(cboFileNameMask.Text, _currentPath, RenameType.TAGFROMFILE, _currentExtension);

            DialogResult result = frm.ShowDialog(this);

            if (result == DialogResult.OK)
            {
                cboFileNameMask.Items.Clear();

                LoadMaskCombo();

                int index = cboFileNameMask.Items.IndexOf(frm.Mask);

                if (index >= 0)
                {
                    cboFileNameMask.SelectedIndex = index;
                }
                else
                {
                    cboFileNameMask.Text = frm.Mask;
                }

                _masks.Mask = frm.Mask;

                RefreshMask();
            }
        }

        private void LoadMaskCombo()
        {
            StringCollection masks = MaskEditorSettings.Default.SavedTagsFromFileMasks;

            if (masks != null)
            {
                foreach (var mask in masks)
                {
                    cboFileNameMask.Items.Add(mask);
                }
            }
        }

        private string ShowPreView(string mask)
        {
            if (string.IsNullOrEmpty(mask))
                return string.Empty;

            string result = mask;

            var helper = new MaskHelperCollection();

            return helper.Aggregate(result, (current, maskHelper) => current.Replace(maskHelper.Mask, maskHelper.Example));
        }

        private void FormPreviewFileName_Resize(object sender, EventArgs e)
        {
            objectListView.BeginUpdate();

            panButtons.Left = Width - (panButtons.Width + 15);
            
            lineBottom.Width = Width - 18;
            
            objectListView.EndUpdate();
        }

        private void objectListView_SelectedIndexChanged(object sender, EventArgs e)
        {
            objectListView.SelectedIndex = -1;
        }

        private void cboFileNameMask_TextChanged(object sender, EventArgs e)
        {
            if(!string.IsNullOrEmpty(cboFileNameMask.Text))
            {
                _masks.Mask = cboFileNameMask.Text;

                RefreshMask();
            }
        }

        #endregion
    }
}
